package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Collecting;
import org.csu.secondhand.api.persistence.CollectingMapper;
import org.csu.secondhand.api.service.CollectingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("collectingService")
public class CollectingServiceImpl implements CollectingService {
    @Autowired
    CollectingMapper collectingMapper;

    @Override
    public Collecting insertCollecting(Collecting collecting) {
        collectingMapper.insert(collecting);
        return collecting;
    }

    @Override
    public Collecting deleteCollecting(String username, long dealing) {
        QueryWrapper<Collecting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("dealing", dealing);
        Collecting collecting = collectingMapper.selectOne(queryWrapper);
        collectingMapper.deleteById(collecting.getId());
        return collecting;
    }

    @Override
    public List<Collecting> getCollectingByUser(String username) {
        QueryWrapper<Collecting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return collectingMapper.selectList(queryWrapper);
    }

    @Override
    public int doICollectIt(String username, long dealing) {
        QueryWrapper<Collecting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("dealing", dealing);
        Collecting collecting = collectingMapper.selectOne(queryWrapper);
        if(collecting == null)
            return 0;
        return 1;
    }
}
